package servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import biz.ShoppingCartBiz;
import biz.impl.ShoppingCartBizImpl;
import entity.ShoppingCart;

/**
 * Servlet implementation class ShoppingCarServlet
 */
@WebServlet("/ShoppingCar")
public class ShoppingCarServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//判断Session里是否存在shoppingCartList
		ShoppingCart shoppingCartList = (ShoppingCart)request.getSession().getAttribute("shoppingCarList");
		
		if(shoppingCartList==null){
			shoppingCartList=new ShoppingCart();
		}
		
		String rateStr = request.getParameter("selectedId");
		int rateId = 0;
		if(rateStr!=null&&!rateStr.isEmpty()){
			rateId=Integer.valueOf(rateStr);
			ShoppingCartBiz cartBiz=new ShoppingCartBizImpl();
			cartBiz.addCartItem(shoppingCartList, rateId);
			HttpSession session = request.getSession();
			session.setAttribute("shoppingCarList", shoppingCartList);
			session.setMaxInactiveInterval(86400);
		
		}
//		response.sendRedirect(request.getContextPath()+"/test.jsp");
//		String type = request.getParameter("type");
//		if(type!=null&&!type.isEmpty()){
//			response.sendRedirect(request.getContextPath()+"/FoodList.html?currentPage=1");
//		}else{
//			response.sendRedirect(request.getContextPath()+"/Index.html");
//		}
		response.sendRedirect(request.getHeader("referer"));
//		Gson gson = new Gson();
//		String json = gson.toJson(rate);
//		response.getWriter().println(json);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
